package com.isoft.mapper;

import com.isoft.entity.DfmeCaneEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeCaneMapper {
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'YYYYMMDDHH24MISS'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId", keyColumn = "MSG_ID")
    void insertMeta(DfmeCaneEntity entity);

    @Update("UPDATE FLIGHT_BASE SET STATUS = 'CANCELLED' WHERE FLIGHT_ID = #{dflt.flightId,jdbcType=NUMERIC}")
    int updateFlightStatus(DfmeCaneEntity entity);

    @Select("SELECT COUNT(1) FROM FLIGHT_BASE WHERE FLIGHT_ID = #{flightId,jdbcType=NUMERIC}")
    int checkFlightExists(Long flightId);

    @Insert("INSERT INTO FLIGHT_BASE(FLIGHT_ID, STATUS) VALUES(#{flightId,jdbcType=NUMERIC}, #{status,jdbcType=VARCHAR})")
    int insertFlightIfNotExists(@Param("flightId") Long flightId, @Param("status") String status);

    // 达梦数据库查询约束条件的SQL - 修改为正确的列名
    @Select("SELECT SEARCH_CONDITION FROM USER_CONSTRAINTS " +
            "WHERE CONSTRAINT_NAME = 'CK_FLIGHT_STATUS' AND TABLE_NAME = 'FLIGHT_BASE'")
    String getFlightStatusConstraint();

    @Select("SELECT STATUS FROM FLIGHT_BASE WHERE ROWNUM = 1")
    String getSampleFlightStatus();

    @Insert({
            "<script>",
            "INSERT INTO FLIGHT_IRREGULARITY(",
            "FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON,",
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS,",
            "MANUAL_ABNORMAL_REASON, MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON)",
            "VALUES(",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{dflt.abnormalStatus,jdbcType=VARCHAR}, ",
            "#{dflt.abnormalReason,jdbcType=VARCHAR}, ",
            "#{dflt.internalAbnormalStatus,jdbcType=VARCHAR}, ",
            "#{dflt.internalAbnormalReason,jdbcType=VARCHAR}, ",
            "#{dflt.manualAbnormalStatus,jdbcType=VARCHAR}, ",
            "#{dflt.manualAbnormalReason,jdbcType=VARCHAR}, ",
            "#{dflt.manualInternalAbnormalStatus,jdbcType=VARCHAR}, ",
            "#{dflt.manualInternalAbnormalReason,jdbcType=VARCHAR})",
            "</script>"
    })
    int insertIrregularity(DfmeCaneEntity entity);
}